﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.Sugar;
using Medianamik.Globalization;
using Medianamik.UI.Web.MVP.Presentation;
using Medianamik.UI.Web.MVP.View;

namespace Medianamik.UI.Web.Shop
{
    public interface IEditAttributeView : INodeTypePropertyEditModuleView
    {
    }

    public partial class ShopEditAttribute : PageModuleViewWithPresenter<ModulePresenter<IEditAttributeView>>, IEditAttributeView
    {
        public event EventHandler OnSave;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            ddlDataTypes.DataTextField = "LocalizedName";
            ddlDataTypes.DataValueField = "Id";

            ddlOptionGroups.DataTextField = "LocalizedName";
            ddlOptionGroups.DataValueField = "ID";

            ddlValidation.DataTextField = "LocalizedName";
            ddlValidation.DataValueField = "Pattern";

            Page.ClientScript.RegisterStartupScript(GetType(), "documentReady", "documentReady();", true);
        }

        public HyperLink CancelButton
        {
            get { return CancelLink; }
        }

        protected void Save(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                OnSave.Raise(this, EventArgs.Empty);
            }
        }

        public bool CanHaveChildren
        {
            get { return false; }
            set { }
        }

        public bool IsGenerated
        {
            get { return false; }
            set { }
        }

        public bool IsAbstract
        {
            get { return false; }
            set { }
        }

        public bool IsListable
        {
            get { return true; }
            set { }
        }

        public bool IsVersioned
        {
            get { return false; }
            set { }
        }

        public IDictionary<string, string> NodeTypePropertyNameValuesByCulture
        {
            get { return TypeNameTranslations.Values; }
            set { TypeNameTranslations.Values = value; }
        }

        public Guid? TypeId
        {
            get { return string.IsNullOrEmpty(QueryString[DefaultParamNames.TypeID]) ? (Guid?)null : new Guid(QueryString[DefaultParamNames.TypeID]); }
        }

        public Guid SelectedDataType
        {
            get
            {
                return new Guid(ddlDataTypes.SelectedValue);
            }
        }

        public bool IsRequired
        {
            get { return cbIsRequired.Checked; }
            set { cbIsRequired.Checked = value; }
        }

        public string SelectedValidationExpression
        {
            get { return ddlValidation.SelectedValue; }
        }

        public Guid SelectedLookup
        {
            get { return new Guid(ddlOptionGroups.SelectedValue); }
        }

        public IList<NodeType> LookupNodeTypes
        {
            get { return (IList<NodeType>)ddlOptionGroups.DataSource; }
            set
            {
                ddlOptionGroups.DataSource = value;
                ddlOptionGroups.DataBind();
                ddlOptionGroups.Items.Insert(0, new ListItem(Translator.Translate("medianamik_typemanagement", "ChooseOptionGroup"), ""));
            }
        }

        public short? MinLength
        {
            get { return string.IsNullOrEmpty(txtMinLength.Text) ? (short?)null : short.Parse(txtMinLength.Text); }
            set { txtMinLength.Text = value.ToString(); }
        }

        public short? MaxLength
        {
            get { return string.IsNullOrEmpty(txtMaxLength.Text) ? (short?)null : short.Parse(txtMaxLength.Text); }
            set { txtMaxLength.Text = value.ToString(); }
        }

        public bool ValuesAreLocalized
        {
            get { return cbValuesAreLocalized.Checked; }
            set { cbValuesAreLocalized.Checked = value; }
        }

        public IDictionary<string, string> TypeNameValuesByCulture
        {
            get { return TypeNameTranslations.Values; }
        }

        public IEnumerable<PropertyDefinition> DataTypes
        {
            get { return (IEnumerable<PropertyDefinition>)ddlDataTypes.DataSource; }
            set
            {
                ddlDataTypes.DataSource = value;
                ddlDataTypes.DataBind();
                ddlDataTypes.Items.Insert(0, new ListItem(Translator.Translate("medianamik_typemanagement", "ChooseType"), ""));
            }
        }

        public IEnumerable<PropertyValidationExpression> ValidationExpression
        {
            set
            {
                ddlValidation.DataSource = value;
                ddlValidation.DataBind();
                ddlValidation.Items.Insert(0, new ListItem(Translator.Translate("medianamik_typemanagement", "NoValidation"), ""));
            }
        }

        protected void ValidateDataType(object source, ServerValidateEventArgs args)
        {
            Guid g;
            args.IsValid = string.IsNullOrEmpty(ddlValidation.SelectedValue) &&
                           ddlValidation.SelectedValue.IsGuidInline(out g);
        }

        protected void ValidateMinLength(object source, ServerValidateEventArgs args)
        {
            if(!string.IsNullOrEmpty(args.Value))
            {
                if(SelectedDataType == PropertyDefinitions.FloatPropertyId)
                {
                    double min;
                    args.IsValid = double.TryParse(args.Value, out min) && min > 0;
                }
                else if (SelectedDataType == PropertyDefinitions.ShortTextPropertyId
                         || SelectedDataType == PropertyDefinitions.IntPropertyId)
                {
                    int min;
                    args.IsValid = int.TryParse(args.Value, out min) && min > 0;
                }
                else
                {
                    txtMinLength.Text = "";
                }
            }
        }

        protected void ValidateMaxLength(object source, ServerValidateEventArgs args)
        {
            if (!string.IsNullOrEmpty(args.Value))
            {
                if (SelectedDataType == PropertyDefinitions.FloatPropertyId)
                {
                    double max;
                    args.IsValid = double.TryParse(args.Value, out max);
                }
                else if (SelectedDataType == PropertyDefinitions.ShortTextPropertyId
                         || SelectedDataType == PropertyDefinitions.IntPropertyId)
                {
                    int max;
                    args.IsValid = int.TryParse(args.Value, out max);
                }
                else
                {
                    txtMaxLength.Text = "";
                }
            }
        }

        protected void ValidateMinAndMaxLength(object source, ServerValidateEventArgs args)
        {
            if (Page.IsValid && !string.IsNullOrEmpty(txtMinLength.Text) && !string.IsNullOrEmpty(txtMaxLength.Text))
            {
                if (SelectedDataType == PropertyDefinitions.FloatPropertyId)
                {
                    args.IsValid = double.Parse(txtMinLength.Text) < double.Parse(txtMaxLength.Text);
                }
                else if (SelectedDataType == PropertyDefinitions.ShortTextPropertyId
                         || SelectedDataType == PropertyDefinitions.IntPropertyId)
                {
                    args.IsValid = int.Parse(txtMinLength.Text) < int.Parse(txtMaxLength.Text);
                }
            }
        }

        protected void ValidateLookup(object source, ServerValidateEventArgs args)
        {
            if(Page.IsValid && SelectedDataType == PropertyDefinitions.OneToManyPropertyId
               || SelectedDataType == PropertyDefinitions.ManyToOnePropertyId)
            {
                args.IsValid = ddlOptionGroups.SelectedIndex > 0;
            }
        }
    }
}